// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        item_prefab: {  //项的资源预制体
            type: cc.Prefab,
            default: null,
        },
        scroll_view: { //获取scrollview组件
            type: cc.ScrollView,
            default: null,
        },
        msg_input_editbox: {
            type: cc.EditBox,
            default: null,
        },
        ServerModule: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    start() {
        this.content = this.scroll_view.content;
        // this.content.size = new cc.Size(1125, 1000);
        this.content.height = 0
        // for (let i = 0; i < 100; i++) {
        //     let item = cc.instantiate(this.item_prefab);
        //     item.getComponent(cc.Label).string = "我是你大爷" + i;
        //     this.content.addChild(item);
        //     console.log( item.height)
        //     this.content.height +=  (item.height+15);
        // }
        this.content.height += 20

        this.ServerModule.getComponent('ServerModule').registerOnSendMsg(this)
    },

    test() {
        console.log("test called");
        this.addOneMsg("test")
    },

    addOneMsg(msg) {
        let item = cc.instantiate(this.item_prefab);
        item.getComponent(cc.Label).string = msg;
        this.content.addChild(item);
        this.content.height += (item.height + 15);
        this.scroll_view.scrollToBottom(0.5);
    },

    onSendMsgBtnClick(event) {
        let msg = this.msg_input_editbox.string
        if ("" == msg || "" == msg.trim()) {
            this.msg_input_editbox.string = ""
            return
        }
        console.log("onSendMsgBtnClick, msg:" + msg)
        this.addOneMsg(msg)
        this.ServerModule.getComponent('ServerModule').sendMsgEvent(msg)
        this.msg_input_editbox.string = ""
    },
});
